#include <string.h>

#include "screens.h"
#include "images.h"
#include "fonts.h"
#include "actions.h"
#include "vars.h"
#include "styles.h"
#include "ui.h"

#include <string.h>

objects_t objects;
lv_obj_t *tick_value_change_obj;

extern volatile double temp;

void action_clear_chart_handle(lv_event_t *e)
{
    lv_chart_set_all_value(objects.chart, objects.chart_series, LV_CHART_POINT_NONE);
}

void update_chart_task(void)
{

    // 模拟温度数据
    int16_t a = temp * 100;
    lv_chart_set_next_value(objects.chart, objects.chart_series, a);

    // 刷新图表
    lv_chart_refresh(objects.chart);
}

void create_screen_main()
{
    lv_obj_t *obj = lv_obj_create(0);
    objects.main = obj;
    lv_obj_set_pos(obj, 0, 0);
    lv_obj_set_size(obj, 480, 320);
    {
        lv_obj_t *parent_obj = obj;
        {
            lv_obj_t *obj = lv_btn_create(parent_obj);
            objects.btn_on_off = obj;
            lv_obj_add_flag(obj, LV_OBJ_FLAG_CHECKABLE);
            lv_obj_set_pos(obj, 57, 262);
            lv_obj_set_size(obj, 89, 34);
            lv_obj_add_event_cb(obj, action_trun_on_of_handle, LV_EVENT_CLICKED, &objects);
            {
                lv_obj_t *parent_obj = obj;
                {
                    lv_obj_t *obj = lv_label_create(parent_obj);
                    objects.btn_label = obj;
                    lv_obj_set_pos(obj, 0, 0);
                    lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);

                    lv_label_set_text(obj, "ON");
                    lv_obj_set_style_align(obj, LV_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
                }
            }
        }
        {
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.value_label = obj;
            lv_obj_set_pos(obj, 338, 272);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, "25.63");
        }
        {
            lv_obj_t *obj = lv_chart_create(parent_obj);
            objects.chart = obj;
            // 设置xy轴指针
            lv_chart_set_axis_tick(obj, LV_CHART_AXIS_PRIMARY_Y, 3, 1, 5, 10, true, 80);
            lv_chart_set_axis_tick(obj, LV_CHART_AXIS_PRIMARY_X, 10, 5, 6, 10, true, 50);
            // 设置y轴最大值
            lv_chart_set_range(obj, LV_CHART_AXIS_PRIMARY_Y, 1000, 5000);
            // 设置x轴最大值
            lv_chart_set_range(obj, LV_CHART_AXIS_PRIMARY_X, 0, 600);

            lv_chart_set_type(obj, LV_CHART_TYPE_LINE); // 线状图
            // lv_obj_set_style_size(obj, 0, LV_PART_INDICATOR); // 设置为折线图        默认为点线图

            lv_chart_set_div_line_count(obj, 12 - 1, 12); // 背景线条，横竖个数
            lv_chart_set_point_count(obj, 600);           // 一个画面绘制的点数

            lv_obj_set_pos(obj, 38, 21);
            lv_obj_set_size(obj, 404, 212);

            // 创建数据序列
            lv_chart_series_t *ser = lv_chart_add_series(obj, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
            objects.chart_series = ser;
        }
        {
            lv_obj_t *obj = lv_btn_create(parent_obj);
            objects.btn_clear = obj;
            lv_obj_set_pos(obj, 197, 263);
            lv_obj_set_size(obj, 89, 34);
            lv_obj_add_event_cb(obj, action_clear_chart_handle, LV_EVENT_CLICKED, &objects);
            {
                lv_obj_t *parent_obj = obj;
                {
                    lv_obj_t *obj = lv_label_create(parent_obj);
                    lv_obj_set_pos(obj, 0, 0);
                    lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
                    lv_label_set_text(obj, "CLEAR");
                    lv_obj_set_style_align(obj, LV_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
                }
            }
        }
    }

    tick_screen_main();
}

void tick_screen_main()
{
}

typedef void (*tick_screen_func_t)();
tick_screen_func_t tick_screen_funcs[] = {
    tick_screen_main,
};
void tick_screen(int screen_index)
{
    tick_screen_funcs[screen_index]();
}
void tick_screen_by_id(enum ScreensEnum screenId)
{
    tick_screen_funcs[screenId - 1]();
}

void create_screens()
{
    lv_disp_t *dispp = lv_disp_get_default();
    lv_theme_t *theme = lv_theme_default_init(dispp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), false, LV_FONT_DEFAULT);
    lv_disp_set_theme(dispp, theme);

    create_screen_main();
}
